package com.white.utils;

import org.springframework.boot.configurationprocessor.json.JSONException;
import org.springframework.boot.configurationprocessor.json.JSONObject;
import org.springframework.stereotype.Component;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;

/**
 * 公用类
 */
@Component
public class Utils {

    /**
     * 将json字符串转为指定的实体类
     * @param clazz 实体类
     * @param json json对象
     * @param <T> 泛型
     * @return 实体类
     */
    public <T> T jsonToEntry(Class<T> clazz, String json) {
        /*参数类型集合*/
        HashMap<String, Class<?>> parameterTypes = new HashMap<>();
        parameterTypes.put("java.lang.Integer", Integer.class);
        parameterTypes.put("int", int.class);
        parameterTypes.put("java.lang.Long", Long.class);
        parameterTypes.put("long", long.class);
        parameterTypes.put("java.lang.Float", Float.class);
        parameterTypes.put("float", float.class);
        parameterTypes.put("java.lang.Double", Double.class);
        parameterTypes.put("double", double.class);
        parameterTypes.put("java.lang.String", String.class);
        T t = null;
        try {
            /*创建对象实例*/
            t = clazz.newInstance();
            JSONObject jsonObject = new JSONObject(json);
            Field[] fields = clazz.getDeclaredFields();
            for (Field field : fields) {
                String fieldName = field.getName();
                String methodName = "set" + fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1);
                /*对于包装类需要加上路径*/
                Class<?> type = field.getType();
                Method method = clazz.getMethod(methodName, parameterTypes.get(type.getName()));
                /*类型转化*/
                Object o = jsonObject.get(fieldName);
                if ("null".equals(o.toString())) {
                    continue;
                }
                if (o instanceof String && ("java.lang.Integer".equals(type.getName()) || "int".equals(type.getName()))) {
                    o = Integer.parseInt((String) o);
                }
                if ("java.lang.Long".equals(type.getName()) || "long".equals(type.getName())) {
                    o = Long.parseLong("" + o);
                }
                method.invoke(t, o);

            }
        } catch (InstantiationException | JSONException | IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
            e.printStackTrace();
        }
        return t;
    }

    /**
     * 检查两天是否连续
     * @param date
     * @param nextDate
     * @return
     */
    public static boolean isNextDay(String date, String nextDate) {
        Date d1 = getDate(date), d2 = getDate(nextDate);
        int defDay = (int) (d2.getTime() - d1.getTime()) / (1000 * 60 * 60 * 24);
        return defDay >= 1 && defDay < 2;
    }

    public static Date getDate(String date) {
        String[] split = date.split(" ")[0].split("-");
        return new Date(Integer.parseInt(split[0]) - 1900, Integer.parseInt(split[1]) - 1, Integer.parseInt(split[2]));
    }

    public static String dateToString(Date date) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        return dateFormat.format(date);
    }

}
